#include <iostream>

using namespace std;

#include "hge\hge.h"
#include "hge\hgesprite.h"
#include "hge\hgeresource.h"

HGE* hge;
hgeResourceManager hrm;
hgeSprite *inner;
hgeSprite *outer;
hgeSprite *halo;
hgeSprite *src;

hgeSprite*			spt;
hgeParticleSystem*	par;


float sx, sy, dx, dy;
float x0 = 0, py0 = 0, x1 = 0, py1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0;

float ix[4], iy[4];
float ox[4], oy[4];
bool lstatus = false;

float scale = 0.0f;

bool FrameFunc()
{
	float dt = hge->Timer_GetDelta();
	static float l = 0.0f;
	if (hge->Input_GetKeyState(HGEK_LBUTTON))
	{
		if (!lstatus) hge->Input_GetMousePos(&sx, &sy);
		else {
			hge->Input_GetMousePos(&dx, &dy);
			float vx, vy, d;
			if (dx == sx) 
			{
				vy = 0;
				vx = 1;
				d = 1;
			}
			else
			{
				vy = 1;
				vx = (sy - dy) / (dx - sx);
				d = sqrt(1 + vx*vx);
			}
			vx /= d;
			vy /= d;

			float iw = 2;
			float ow = 5;

			ix[0] = dx + vx * iw;
			iy[0] = dy + vy * iw;

			ix[1] = dx - vx * iw;
			iy[1] = dy - vy * iw;

			ix[2] = sx - vx * iw;
			iy[2] = sy - vy * iw;

			ix[3] = sx + vx * iw;
			iy[3] = sy + vy * iw;

			ox[0] = dx + vx * ow;
			oy[0] = dy + vy * ow;

			ox[1] = dx - vx * ow;
			oy[1] = dy - vy * ow;

			ox[2] = sx - vx * ow;
			oy[2] = sy - vy * ow;

			ox[3] = sx + vx * ow;
			oy[3] = sy + vy * ow;
		}
	}
	//float cx = 
	l += 0.1f * dt;
	float cx = (dx - sx) * l + sx;
	float cy = (dy - sy) * l + sy;

	if (l >= 1.0f) l = 0.0f;
	par->MoveTo(cx, cy);

	lstatus = hge->Input_GetKeyState(HGEK_LBUTTON);
	return false;
}

bool RenderFunc()
{
	hge->Gfx_BeginScene();
	hge->Gfx_Clear(0);
	par->Render();
	outer->Render4V(ox[0], oy[0], ox[1], oy[1], ox[2], oy[2], ox[3], oy[3]);
	inner->Render4V(ix[0], iy[0], ix[1], iy[1], ix[2], iy[2], ix[3], iy[3]);
	//halo->Render(dx, dy);
	//src->Render(sx, sy);
	hge->Gfx_EndScene();
	return false;
}

int main()
{
	memset(ix, 0, sizeof(ix));
	memset(iy, 0, sizeof(iy));

	memset(ox, 0, sizeof(ox));
	memset(oy, 0, sizeof(oy));

	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE, "bulletTest.log");
	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
	hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
	hge->System_SetState(HGE_TITLE, "A.I.R bulletEffect");
	hge->System_SetState(HGE_WINDOWED, true);
	hge->System_SetState(HGE_SCREENWIDTH, 800);
	hge->System_SetState(HGE_SCREENHEIGHT, 600);
	hge->System_SetState(HGE_SCREENBPP, 32);
	hge->System_SetState(HGE_HIDEMOUSE, false);
	hge->System_SetState(HGE_SHOWSPLASH, false);

	if(hge->System_Initiate())
	{
		hrm.ChangeScript("aeria.res");
		hrm.Precache();


		//halo = new hgeSprite(/*hrm.GetTexture("shield")*/0, 96.f, 64.f, 32.f, 32.f);
		//halo->SetColor(0xafffff00);
		//halo->SetHotSpot(16.f, 16.f);

		inner = new hgeSprite(0, 0, 0, 10, 10);
		inner->SetColor(0xff000000);
		outer = new hgeSprite(0, 0, 0, 10, 10);
		outer->SetColor(0xafff0000);
		//src = new hgeSprite(0, 0, 0, 32.f, 32.f);
		//src->SetColor(0xafffff00);
		//src->SetHotSpot(16.f, 16.f);

		spt = new hgeSprite(hrm.GetTexture("shield"), 64, 64, 32, 32);
		//spt->SetColor(ARGB(1.);

		spt->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
		spt->SetHotSpot(16,16);
		spt->SetColor(0xffff0000);

		par=new hgeParticleSystem("particle5.end.psi",spt);
		par->info.colColorStart.r = -1;
		par->info.colColorEnd.r = -1;
		par->Fire();

		hge->System_Start();

		delete par;
		delete spt;
		
		delete inner;
		delete outer;

		hrm.Purge();
	}
	hge->System_Shutdown();
	hge->Release();
	return 0;
}